In [1]:
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import yfinance as yf
import datetime
from datetime import date, timedelta
In [2]:
today=date.today()
In [3]:
d1= today.strftime("%Y-%m-%d")
In [4]:
end_date=d1
In [5]:
d2= date.today()-timedelta(days=365)
In [6]:
d2= d2.strftime("%Y-%m-%d")
In [7]:
start_date=d2
In [8]:
data=yf.download('GOOG', start=start_date,end=end_date, progress=False)
In [9]:
data.head()
Out[9]:
Open High Low Close Adj Close Volume
Date
2021-10-19 00:00:00-04:00 143.291504 144.106995 143.095993 143.822006 143.822006 15316000
2021-10-20 00:00:00-04:00 144.222504 144.247757 141.912003 142.414993 142.414993 17940000
2021-10-21 00:00:00-04:00 142.192001 142.849503 141.636993 142.780502 142.780502 14850000
2021-10-22 00:00:00-04:00 140.350998 141.558502 137.170502 138.625000 138.625000 30182000
2021-10-25 00:00:00-04:00 138.810501 139.205795 136.748505 138.772995 138.772995 21082000
In [10]:
data.shape
Out[10]:
(252, 6)
In [11]:
data["Date"]=data.index
In [12]:
data = data[["Date","Open","High","Low","Close","Adj Close","Volume"]]
In [13]:
data.reset_index(drop=True, inplace=True)
data.head()
Out[13]:
Date Open High Low Close Adj Close Volume
0 2021-10-19 00:00:00-04:00 143.291504 144.106995 143.095993 143.822006 143.822006 15316000
1 2021-10-20 00:00:00-04:00 144.222504 144.247757 141.912003 142.414993 142.414993 17940000
2 2021-10-21 00:00:00-04:00 142.192001 142.849503 141.636993 142.780502 142.780502 14850000
3 2021-10-22 00:00:00-04:00 140.350998 141.558502 137.170502 138.625000 138.625000 30182000
4 2021-10-25 00:00:00-04:00 138.810501 139.205795 136.748505 138.772995 138.772995 21082000

Candlestick Visualization of Google's stock price¶

In [14]:
figure=go.Figure(data=[go.Candlestick(x=data["Date"],open=data["Open"],high=data["High"],low=data["Low"],close=data["Close"])])
In [15]:
figure.update_layout(title="Google Stock Price Analysis", xaxis_rangeslider_visible=False)
figure.show()

Bar Plot is a handy tool to Visualize stocks¶

In [16]:
figure =px.bar(data,x='Date',y='Close')
figure.show()

Stock analysis with rangeslider¶

In [17]:
figure=px.line(data,x='Date',y='Close', title='Stock market analysis with rangeslider')
figure.update_xaxes(rangeslider_visible=True)
figure.show()

Time period selectors¶

In [22]:
figure=px.line(data,x='Date',y='Close', title='Stock market analysis with Time Period Selectors')
figure.update_xaxes(
    rangeselector=dict(
    buttons=list([
        dict(count=1,label='1m',step='month',stepmode='backward'),
        dict(count=6,label='6m',step='month',stepmode='backward'),
        dict(count=3,label='3m',step='month',stepmode='backward'),
        dict(count=1,label='1y',step='year',stepmode='backward'),
        dict(step="all")
    ])
    )
)
figure.show()

Stock Analysis by removing weekend gaps¶

In [23]:
figure=px.scatter(data, x='Date', y='Close', range_x=['2021-10-20', '2022-10-18'],
                 title="Stock market analysis by hiding weekend gaps")
figure.update_xaxes(
    rangebreaks=[
        dict(bounds=['sat','sun'])
    ]
)
figure.show()
In [ ]: